/**
* AutoSWF - Cody Cushing. March 26, 2009
* Copyright (c) 2009 Cody Cushing
* No use restrictions. Maintain this notice in all versions. 
**/

package com.autoswf.taskrunner
{
	import flexunit.framework.TestCase;

	public class TaskModelTest extends TestCase
	{
		private var taskRunner:MockTaskRunner;
		private var taskModel:TaskModel;
		
		public function TaskModelTest(methodName:String=null)
		{
			super(methodName);
		}
		
		override public function setUp():void
		{
			taskRunner = new MockTaskRunner();
			taskModel = new TaskModel(taskRunner);
		}
		
		/**
		 * Test that requests are delegated to a TaskRunner
		 */		
		public function test01():void
		{
			var request:MockRequest = new MockRequest();
			
			request.mock.property("id").andReturn("testId").twice;
			taskRunner.mock.method("addLoadTask").withArgs(request).once; /* primary test */
			
			taskModel.addTask(request);
			
			assertTrue(taskRunner.mock.verify());
		}
		
		/**
		 * Test that a completed request is passed to the complete handler.
		 */		
		public function test02():void
		{
			var request:MockRequest = new MockRequest();
			var completeHandler:Function = function(req:MockRequest):void
			{
				assertTrue("request not passed to completeHandler", req === request); /* primary test */
			}
			request.mock.property("id").andReturn("testId").twice;
			request.mock.property("data").withArgs("dataVal").once;
			request.mock.property("completeHandler").andReturn(completeHandler).twice;
			
			taskRunner.mock.method("addLoadTask").withArgs(request).once;
			taskRunner.mock.method("data").withArgs("testId").andReturn( "dataVal" ).once;
			
			taskModel.addTask(request);
			
			taskRunner.dispatchEvent(new TaskEvent(TaskEvent.TASK_COMPLETE, "testId"));
			
			assertTrue(taskRunner.mock.verify() && request.mock.verify());
		}
		
		/**
		 * Test that a completed request is passed to multiple complete handlers.
		 */		
		public function test03():void
		{
			var request01:MockRequest = new MockRequest();
			var completeHandler01:Function = function(req:MockRequest):void
			{
				assertTrue("request01 not passed to correct handler", req === request01);  /* secondary test */
			}
			request01.mock.property("id").andReturn("testId").twice;
			request01.mock.property("completeHandler").andReturn(completeHandler01).twice;
			request01.mock.property("data").withArgs("dataVal").once;
			
			
			var request02:MockRequest = new MockRequest();
			var completeHandler02:Function = function(req:MockRequest):void
			{
				assertTrue("request02 not passed to correct handler", req === request02);  /* secondary test */
			}
			request02.mock.property("id").andReturn("testId").twice;
			request02.mock.property("completeHandler").andReturn(completeHandler02).twice;
			request02.mock.property("data").withArgs("dataVal").once;
			
			
			taskRunner.mock.method("addLoadTask").withArgs(request01).once; /* primary test */
			taskRunner.mock.method("data").withArgs("testId").andReturn("dataVal").once;
			
			taskModel.addTask(request01);
			taskModel.addTask(request02);
			
			taskRunner.dispatchEvent(new TaskEvent(TaskEvent.TASK_COMPLETE, "testId"));
			
			assertTrue(taskRunner.mock.verify() && request01.mock.verify() && request02.mock.verify());
		}
		
		/**
		 * Test that failed requests are passed to the errorHandler.
		 */		
		public function test04():void
		{
			var request:MockRequest = new MockRequest();
			var errorHandler:Function = function(req:MockRequest):void
			{
				assertTrue("request not passed to completeHandler", req === request); /* secondary test */
			}
			request.mock.property("id").andReturn("testId").twice;
			request.mock.property("errorHandler").andReturn(errorHandler).twice;  /* primary test */
			
			taskRunner.mock.method("addLoadTask").withArgs(request).once;
			
			taskModel.addTask(request);
			
			taskRunner.dispatchEvent(new TaskEvent(TaskEvent.TASK_FAILURE, "testId"));
			
			assertTrue(taskRunner.mock.verify() && request.mock.verify());
		}
		
	}
}

